using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;

public class XmlDataMgr 
{
    private static XmlDataMgr instance=new XmlDataMgr();
    public static XmlDataMgr Instance => instance;

    private XmlDataMgr() { } 

    public void SaveData(object obj,string filename)
    {
        string path = Application.persistentDataPath + "/" + filename + ".xml";
        using (StreamWriter s=new StreamWriter(path))
        {
            XmlSerializer x=new XmlSerializer(obj.GetType());
            x.Serialize(s,obj);
        }
    }

    public object LoadData(Type type, string filename)
    {
        string path = Application.persistentDataPath + "/" + filename + ".xml";
        if(!File.Exists(path))
        {
            path= Application.streamingAssetsPath +"/" + filename + ".xml";
            if(!File.Exists(path))
                return Activator.CreateInstance(type);
        }
        using (StreamReader s = new StreamReader(path))
        {
            XmlSerializer xml=new XmlSerializer(type);
            return xml.Deserialize(s);
        }
    }
}
